package com.example.exp03.Controller;


import com.example.exp03.Model.RegionStats;
import com.example.exp03.Service.Mapp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DataController {

       @RequestMapping("/")
       public String showAllCases(Model model) throws IOException {
              int total=0;
              int pertotal=0;
              List<RegionStats> allcases= Mapp.getAllCoronaVirusData();
              for(RegionStats r:allcases){
                     total+=r.getLatestTotalCases();
                     pertotal+=r.getDiffFromPrevDay();
              }
              model.addAttribute("total",total);
              model.addAttribute("pertotal",pertotal);
              model.addAttribute("allcases",allcases);
              return "showAllCases";

       }
       @RequestMapping("/search")
       public String search(HttpServletRequest request,Model model) throws IOException {
              String country = request.getParameter("country");
              List<RegionStats> list=new ArrayList<>();
              int total=0;
              int pertotal=0;
              for (RegionStats r:Mapp.allCoronaVirusData){
                     if(r.getCountry().equals(country)){
                            total+=r.getLatestTotalCases();
                            pertotal+=r.getDiffFromPrevDay();
                            list.add(r);
                     }
              }
              model.addAttribute("total", total);
              model.addAttribute("pertotal", pertotal);
              model.addAttribute("list", list);
              return "search";
       }
}
